/**
 * Filename: FindActivity.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/7/9 2:57
 */
package com.github.postsanf.yinian.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.BaseApplication;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.adapter.BannerAdapter;
import com.github.postsanf.yinian.bean.YNBanner;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.ISkinUpdate;
import com.github.postsanf.yinian.service.AlbumService;
import com.github.postsanf.yinian.utils.DisplayUtils;
import com.github.postsanf.yinian.utils.SkinPackageManager;
import com.github.postsanf.yinian.view.CirclePageIndicator;
import com.github.postsanf.yinian.view.LoopViewPager;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

/**
 * 发现页面
 */
public class FindActivity extends BaseActivity implements View.OnClickListener, ISkinUpdate {
    private Toolbar mToolbar;
    private LinearLayout find_memory_card;
    private LinearLayout find_time_card;
    private LinearLayout find_public_album;
    private LinearLayout find_today_memory;
    private LinearLayout find_whats_yn;
    private BannerAdapter mBannerAdapter;
    private LoopViewPager adViewPager;
    private CirclePageIndicator mPagerIndicator;             // 指示器
    private List<YNBanner> adList = new ArrayList<>();
    private ImageView iv_time_goods_new;
    private Intent bIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yn_activity_find);
        BaseApplication.getInstance().mActivitys.add(this);
        registerBroadcastReceiver();
        initAdData();
        initView();
        noticeBannerInit();
    }

    private void initAdData() {
        adViewPager = (LoopViewPager) findViewById(R.id.vp_home_banner);
        mPagerIndicator = (CirclePageIndicator) findViewById(R.id.banner_indicator);
        int width = DisplayUtils.getScreenWidthPixels(this);
        adViewPager.getLayoutParams().height = 4 * width / 9;
        adViewPager.requestLayout();
    }

    // 注册接收者
    private void registerBroadcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction(ACTIONs.actionBannerUpdate);
        registerReceiver(mCameraBroadcastReceiver, myIntentFilter);
    }

    // 分析并执行动作
    BroadcastReceiver mCameraBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case ACTIONs.actionBannerUpdate:
                    List<YNBanner> list = (List<YNBanner>) intent.getExtras().getSerializable(ACTIONs.aAddData);
                    adList.clear();
                    adList.addAll(list);
                    if (adList.size() > 0) {
                        mBannerAdapter = new BannerAdapter(FindActivity.this, adList);
                        adViewPager.setAdapter(mBannerAdapter);
                        mPagerIndicator.setViewPager(adViewPager);
                    }
                    break;
            }
        }
    };

    private void noticeBannerInit() {
        bIntent = new Intent(this, AlbumService.class);
        bIntent.setAction(ACTIONs.actionBanner);
        startService(bIntent);
    }

    private void initView() {
        mToolbar = (Toolbar) findViewById(R.id.find_toolbar);
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(R.drawable.ic_return);
        find_memory_card = (LinearLayout) findViewById(R.id.find_memory_card);
        find_time_card = (LinearLayout) findViewById(R.id.find_time_card);
        find_public_album = (LinearLayout) findViewById(R.id.find_public_album);
        find_today_memory = (LinearLayout) findViewById(R.id.find_today_memory);
        find_whats_yn = (LinearLayout) findViewById(R.id.find_whats_yn);
        iv_time_goods_new = (ImageView) findViewById(R.id.iv_time_goods_new);
        findViewById(R.id.find_time_goods).setOnClickListener(this);
        find_memory_card.setOnClickListener(this);
        find_time_card.setOnClickListener(this);
        find_public_album.setOnClickListener(this);
        find_today_memory.setOnClickListener(this);
        find_whats_yn.setOnClickListener(this);
        SharedPreferences tgFrist = getSharedPreferences("TimeGoodsNew", 0);
        SharedPreferences.Editor editor = tgFrist.edit();
        if (tgFrist.getBoolean("IsStart", false)) {
            iv_time_goods_new.setVisibility(View.GONE);
        } else {
            iv_time_goods_new.setVisibility(View.VISIBLE);
            editor.putBoolean("IsStart", true);
            editor.commit();
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
        overridePendingTransition(R.anim.slide_in_left2, R.anim.slide_out_right2);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                overridePendingTransition(R.anim.slide_in_left2, R.anim.slide_out_right2);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            // 时光罗盘
//            case R.id.iv_time_compass:
//                intent2Activity(YNTimeLineActivity.class);
//                break;
            // 记录卡片
            case R.id.find_memory_card:
                SharedPreferences mcFrist = getSharedPreferences("MemoryCardFrist", 0);
                SharedPreferences.Editor editor = mcFrist.edit();
                if (mcFrist.getBoolean("IsStart", false)) {
                    startActivity(new Intent(FindActivity.this, LocalMemoryCardManageActivity.class).
                            putExtra(ACTIONs.IstoSync, true));
                } else {
                    startActivity(new Intent(FindActivity.this, MemoryCardGuideActivity.class).
                            putExtra(ACTIONs.IstoSync, true));
                    editor.putBoolean("IsStart", true);
                    editor.commit();
                }
                break;
            // 时光明信片
            case R.id.find_time_card:
                startActivity(new Intent(FindActivity.this, LocalTimeCardManageActivity.class).
                        putExtra(ACTIONs.IstoSync, true));
                break;
            // 公开相册
            case R.id.find_public_album:
                intent2Activity(OfficialAlbumListActivity.class);
                break;
            // 今日忆
            case R.id.find_today_memory:
                intent2Activity(TodayShowActivity.class);
                break;
            // 什么是忆年
            case R.id.find_whats_yn:
                intent2Activity(WhatsYNActivity.class);
                break;
            case R.id.find_time_goods:
                intent2Activity(TimeGoodsWebActivity.class);
                break;
        }
    }

    @Override
    public void updateTheme() {
        if (mToolbar != null) {
            try {
                Resources mResource = SkinPackageManager.getInstance(this).mResources;
                mToolbar.setNavigationIcon(mResource.getDrawable(mResource.getIdentifier("ic_return", "drawable", "com.github.postsanf.skinby")));
                mToolbar.setBackgroundColor(mResource.getColor(mResource.getIdentifier("yncolor", "color", "com.github.postsanf.skinby")));
                mToolbar.setTitleTextColor(mResource.getColor(mResource.getIdentifier("border_color", "color", "com.github.postsanf.skinby")));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        stopService(bIntent);
        BaseApplication.getInstance().mActivitys.remove(this);
        unregisterReceiver(mCameraBroadcastReceiver);
    }

    // 友盟统计
    public void onResume() {
        super.onResume();
        if (SkinPackageManager.getInstance(this).mResources != null) {
            updateTheme();
        }
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}
